<template>
  <div class="container">
    <div class="app-container">
      <!-- 角色管理内容 -->
      <div class="role-operate">
        <el-button size="mini" type="primary" @click="showDialog = true">添加角色</el-button>
      </div>
      <!-- 放置table组件 -->
      <el-table :data="list">
        <!-- 放置列 -->
        <el-table-column align="center" width="200" label="角色">
          <template slot-scope="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.name" type="text" />
            <span v-else>{{ row.name }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" width="200" label="启用">
          <template slot-scope="{row}">
            <el-switch
              v-if="row.isEdit"
              v-model="row.editRow.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              node-key="row.id"
              :default-expanded-keys="[1]"
              :active-value="1"
              :inactive-value="0"
            />
            <span v-else>  {{ row.state === 1 ? '已启用' : '禁用中' }}</span>
          </template>
        </el-table-column>

        <el-table-column align="center" label="描述">
          <template slot-scope="{row}">
            <el-input v-if="row.isEdit" v-model="row.editRow.description" type="textarea" />
            <span v-else> {{ row.description }} </span>
          </template>
        </el-table-column>

        <el-table-column fixed="right" label="操作" width="200">
          <template slot-scope="{row}">
            <template v-if="row.isEdit">
              <el-button type="text" size="small" @click="hOk(row)">确认</el-button>
              <el-button type="text" size="small" @click="row.isEdit = false">取消</el-button>
            </template>
            <template v-else>
              <el-button type="text" size="small" @click="ShowDialogss(row.id)">分配权限</el-button>
              <el-button type="text" size="small" @click="row.isEdit = true">编辑</el-button>
              <!-- <el-button type="text" size="small">删除</el-button> -->
              <el-popconfirm
                confirm-button-text="好的"
                cancel-button-text="不用了"
                icon="el-icon-info"
                icon-color="red"
                title="这是一段内容确定删除吗？"
                @onConfirm="del(row.id)"
              >
                <el-button slot="reference" type="text" size="small"> 删除</el-button>
              </el-popconfirm>
            </template>
          </template>
        </el-table-column>
      </el-table>
        <AssignPermission 
          ref="assign"
          :isShowAssignDialog.sync="isShowAssignDialog"
          :curId="curId"
          />
      <!-- 放置分页组件 -->
      <el-row type="flex" style="height:60px" align="middle" justify="end">
        <!-- 放置分页组件 -->
        <el-pagination
          background
          layout="sizes, prev, pager, next"
          :current-page="page"
          :total="total"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="10"
          @current-change="currentChage"
          @size-change="currentChages"
        />
      </el-row>

      <el-dialog
        title="提示"
        :visible.sync="showDialog"
        width="30%"
      >
        <!-- :before-close="handleClose" -->
        <el-form
          ref="formData"
          :rules="rules"
          :model="formData"
          label-width="80px"
        >
          <el-form-item label="角色" prop="name">
            <el-input v-model="formData.name" />
          </el-form-item>
          <el-form-item label="启用">
            <el-switch
              v-model="formData.state"
              active-color="#13ce66"
              inactive-color="#ff4949"
              active-value="1"
              inactive-value="0"
            />
          </el-form-item>
          <el-form-item label="描述" prop="description">
            <el-input v-model="formData.description" type="textarea" />
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="showDialog = false">取 消</el-button>
          <el-button type="primary" @click="hClose">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>
<script>
import { addRoleList, getRoleList, updataRole, delRole } from '@/api/role'
import AssignPermission from './components/assignPermission.vue'
export default {
  components: { AssignPermission },
  name: 'Role',
  data() {
    return {
      formData: {
        name: '',
        state: 1,
        description: ''
      },
      rules: {
        name: [
          { required: true, message: '请输入员工姓名', trigger: ['blur', 'change'] },
          { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ]
      },
      showDialog: false,
      isShowAssignDialog: false,
      curId: -1,
      list: [],
      total: 0,
      page: 1,
      pagesize: 10,
      num: 1
    }
  },
  created() {
    this.loadRoleList()
  },
  methods: {
    ShowDialogss(id) {
      this.curId = id
      this.isShowAssignDialog = true
      this.$nextTick(() => {
        this.$refs.assign.getPermiss()
      })
    },
    async del(id) {
      await delRole(id)
      this.$message.success('删除成功')
      if (this.list.length === 1) { if (this.page > 1) this.page-- }
      this.loadRoleList()
    },
    async hOk(row) {
      if (!row.editRow.name || !row.editRow.description) {
        this.$message.error('内容不能为空')
        return
      }
      // console.log(row.editRow)
      await updataRole(row.editRow)
      this.$message.success('更新成功')
      // eslint-disable-next-line require-atomic-updates
      row.idEdit = false
      this.loadRoleList()
    },
    async hClose() {
      this.$refs.formData.validate()
      await addRoleList(this.formData)
      this.$message.success('登录成功')
      this.total = this.total + 1
      this.page = Math.ceil(this.total / this.pagesize)
      this.showDialog = false
      this.loadRoleList()
    },
    currentChages(pagesize) {
      this.pagesize = pagesize
      this.page = 1
      this.loadRoleList()
    },
    currentChage(page) {
      this.page = page
      this.loadRoleList()
    },
    async loadRoleList() {
      const res = await getRoleList({ page: this.page, pagesize: this.pagesize })
      // console.log(res.data.rows)
      this.list = res.data.rows
      // console.log(this.list)
      this.list.forEach(item => {
        this.$set(item, 'isEdit', false)
        this.$set(item, 'editRow', {
          id: item.id,
          name: item.name,
          state: item.state,
          description: item.description
        })
        // console.log(item.row.id)
      })
      this.total = +res.data.total
    }
  }
}
</script>
<style scoped>
.role-operate {
  padding: 10px;
}
</style>

